<?php
class Search_Model_Yachinsouba extends Apm_Model_Base{

	/**
	* 家賃相場表示日を取得
	*
	* @access	public
	* @param	array $params
	* @return	string yyyyMMdd
	*/
	public function getYachinsoubaDate()
	{
		$sql  = "";
		if (APPLICATION_ENV == 'production') {
			// 本番は前日または前々日（日次で家賃相場が集計されるまでのブランクを保護）
			$sql .= "SELECT MAX(A02_TARGET_YMD) as YMD FROM A02ESTATE_MKT_PRICE_DAY30 WHERE A02_TARGET_YMD between to_number(to_char(sysdate-2,'yyyyMMdd')) and to_number(to_char(sysdate-1,'yyyyMMdd'))";
		} else {
			$sql .= "SELECT MAX(A02_TARGET_YMD) as YMD FROM A02ESTATE_MKT_PRICE_DAY30 WHERE A02_TARGET_YMD < to_number(to_char(sysdate,'yyyyMMdd'))";
		}
		return $this->dbGetData($sql, array());
	}

	/**
	* 家賃相場を表示用に変換
	*
	* @access	public
	* @param	array ※参照渡し
	* @return	void
	*/
	public function convertYachinsouba($result, $params, &$data)
	{
		$maxPrice = array();
		$minPrice = array();
		
		$par_name = "";
		$soba = array();
		foreach ($result as $value) {
			$key = $value['KEY_CD'];
			$par_name = $value['PAR_NAME'];
			if (empty($soba[$key])) {
				$soba[$key] = array();
				$soba[$key]['KEY_CD']   = $key;
				$soba[$key]['PAR_NAME'] = $value['PAR_NAME'];
				$soba[$key]['KEY_NAME'] = $value['KEY_NAME'];
				$soba[$key]['SOBA'] = array();
			}
			$avg_data = array();
			$avg_data['YACHIN_AVG'] = sprintf("%.2f", $value['YACHIN_AVG']/ 10000);
			$avg_data['MAX_FLG'] = 0;
			$avg_data['MIN_FLG'] = 0;
			$soba[$key]['SOBA'][$value['MADORI']] = $avg_data;
			
			// 最大値・最小値
			$ave = sprintf("%.9f", $value['YACHIN_AVG']/ 10000);
			if (empty($maxPrice[$value['MADORI']])) {
				$maxPrice[$value['MADORI']] = array($ave, $key);
			} else if ($maxPrice[$value['MADORI']][0] < $ave) {
				$maxPrice[$value['MADORI']] = array($ave, $key);
			} else if ($maxPrice[$value['MADORI']][0] == $ave) {
				array_push($maxPrice, $key);
			}
			if (empty($minPrice[$value['MADORI']])) {
				$minPrice[$value['MADORI']] = array($ave, $key);
			} else if ($minPrice[$value['MADORI']][0] > $ave) {
				$minPrice[$value['MADORI']] = array($ave, $key);
			} else if ($minPrice[$value['MADORI']][0] == $ave) {
				array_push($minPrice, $key);
			}
		}
		
		// 最大値・最小値フラグ立て
		foreach ($maxPrice as $key => $val) {
			if (count($val) > 1) {
				for ($i=1; $i<count($val); $i++) {
					$soba[$val[$i]]['SOBA'][$key]['MAX_FLG'] = 1;
				}
			}
		}
		foreach ($minPrice as $key => $val) {
			if (count($val) > 1) {
				for ($i=1; $i<count($val); $i++) {
					$soba[$val[$i]]['SOBA'][$key]['MIN_FLG'] = 1;
				}
			}
		}
		
		$data['yachinsouba'] = $soba;
		$data['PAR_NAME'] = $par_name;
	}

	/**
	* 家賃相場を表示用に変換
	*
	* @access	public
	* @param	array ※参照渡し
	* @return	void
	*/
	public function convertYachinsoubaSyosai($result, $result_graph, &$data)
	{
		$key = "";
		$par_name = "";
		$key_name = "";
		$soba = array(
				  '1' => array('NAME'=>'家賃')
				, '2' => array('NAME'=>'坪賃料')
				, '3' => array('NAME'=>'敷金')
				, '4' => array('NAME'=>'礼金')
			);
		foreach ($result as $value) {
			$key = $value['KEY_CD'];
			$par_name = $value['PAR_NAME'];
			$key_name = $value['KEY_NAME'];
			
			$soba['1'][$value['MADORI']] = sprintf("%.2f", $value['YACHIN_AVG']/ 10000);
			$soba['2'][$value['MADORI']] = sprintf("%.2f", $value['TSUBOYACHIN_AVG']/ 10000);
			$soba['3'][$value['MADORI']] = sprintf("%.2f", $value['SHIKIKIN_AVG']/ 10000);
			$soba['4'][$value['MADORI']] = sprintf("%.2f", $value['REIKIN_AVG']/ 10000);
		}
		
		$g_flag = 0;
		$g_data = array();
		if (!empty($result_graph)) {
			// グラフ加工
			$g_data['graph'] = $result_graph;
			
			//グラフ縦幅の等分数
			$setcount = 5;
			$count = 0;
			$price=Array();
			foreach ($result_graph as $record){
				$price[$count] = $record['PRICE'];
				$count++;
			}
			//値段の最大値
			if (!empty($price)) {
				$max_price = max($price);
			} else {
				$max_price = 100;
			}
			
			//縦軸の最大値
			$gridmax = 0;
			if($max_price < 10000){
				//最大値が1万円未満
				$gridmax = 1;
			}else{
				for($i=$setcount; $i<=1000; $i=$i+$setcount){
					if($max_price < $i * 10000){
						$gridmax = $i;
						break;
					}
				}
			}
			$interval = $gridmax / $setcount;
			
			//縦軸ラベル
			$label = Array();
			for($i=0; $i<=$setcount; $i++){
				$label[$i] = $i * $interval;
			}
			$g_data['label_y'] =implode("|",$label);
			$g_data['graph_range_y'] = 100 / $setcount;
			
			$count = 0;
			$price_prot=Array();
			foreach ($result_graph as $record){
				$price_prot[$count] = $record['PRICE'] / ($gridmax * 100);
				$count++;
			}
			$g_data['graph_y'] = implode(",",$price_prot);
			$g_flag = 1;
		}
		
		$data['yachinsouba'] = array(
				  'KEY_CD' => $key
				, 'PAR_NAME' => $par_name
				, 'KEY_NAME' => $key_name
				, 'SOBA' => $soba
				, 'GRAPH' => $g_flag
				, 'GRAPH_DATA' => $g_data
			);
		$data['PAR_NAME'] = $par_name.$key_name;
	}

	/**
     * ソート関数
     *
     * ソートを行う
     * @access	public
     * @param	array
     */
    public function sortInfo($params,&$lineHash,$lineHash2)
    {

		$madoricode_matrix = $params['madoricode_matrix'];
		$sortflag = $params['sortflag'];
		$madori = Apm_Config_Search::$MADORI_NAME_LIST[$madoricode_matrix];

    	if($madoricode_matrix <> '0'){
    		//賃料ソートフラグ 間取り単位昇順のとき
    		if($sortflag == 1){
				asort($lineHash2[$madori],SORT_NUMERIC);
				// 連想配列からキーを取得。
				$keys = array_keys($lineHash2[$madori]);
				$defalut_keys = array_keys($lineHash);

				$lineHash3 = array();

				foreach($defalut_keys as $key => $value){
					if($lineHash[$value][$madori]['avg'] == ''){
						$lineHash3[$value][$madori]['avg'] = '';
					}
				}

				foreach($keys as $citycode){
					foreach($lineHash as $in_citycode =>$value){
						if($in_citycode == $citycode){
							$lineHash3[$citycode] = $value;
						}
					}
				}
				$lineHash = $lineHash3;
    		}else{
				//賃料ソートフラグ 間取り単位降順のとき
				arsort($lineHash2[$madori],SORT_NUMERIC);
				// 連想配列からキーを取得。
				$keys = array_keys($lineHash2[$madori]);
				$defalut_keys = array_keys($lineHash);

				$lineHash3 = array();

    			foreach($keys as $citycode){
					foreach($lineHash as $in_citycode =>$value){
						if($in_citycode == $citycode){
							$lineHash3[$citycode] = $value;
						}
					}
				}

    			foreach($defalut_keys as $key => $value){
					if($lineHash[$value][$madori]['avg'] == ''){
						$lineHash3[$value][$madori]['avg'] = '';
					}
				}

				$lineHash = $lineHash3;
    		}
    	}else{
    		//市区町村コード 間取り単位昇順のとき
    		if($sortflag == 1){
				//何もしない（デフォルト）
    		//市区町村コード 間取り単位降順のとき
    		}else{
    			krsort($lineHash,SORT_NUMERIC);
    		}
    	}
    }


   /**
	* 家賃相場（市区町村）を表示用に変換
	*
	* @access	public
	* @param	array ※参照渡し
	* @return	void
	*/
	public function convertYachinsoubaData($result,$params,&$data)
	{

			$defalut_keys = array();
			foreach ($result as $record){
				$citycode = $record['A02_KEY_CD'];
				$madori = $record['MADORI'];
				$yachin = $record['YACHIN_AVG'];
				$lineHash[$citycode][$madori]['avg'] = sprintf("%.2f", $yachin/ 10000);
			}

			$lineHash2 = array();
			foreach ($result as $record){
				$citycode = $record['A02_KEY_CD'];
				$madori = $record['MADORI'];
				$yachin = $record['YACHIN_AVG'];
				$lineHash2[$madori][$citycode] = sprintf("%.9f", $yachin/ 10000);
			}

			//間取り列ソート
			$this->sortInfo($params,$lineHash,$lineHash2);

			$maxPrice = array();
			foreach($lineHash2 as $madori => $value){
				$array = $lineHash2[$madori];
				$max = 0;
				$counter = 0;
				foreach($array as $key => $value2){
					if($max < $value2){
						$max = $value2;
						$maxPrice[$madori] =  $key;
					}
					$counter++;
				}
			}

			$minPrice = array();
			foreach($lineHash2 as $madori => $value){
				$array = $lineHash2[$madori];
				$min = 0.0;
				$counter = 0;
				foreach($array as $key => $value2){
					if($counter == 0){
						$minPrice[$madori] =  $key;
						$min = $key;
					}
					if($value2 < $min && $value2 <> ""){
						$min = $value2;
						$minPrice[$madori] =  $key;
					}
					$counter++;
				}
			}

			$data['yachinsouba'] = $lineHash;
			$data['maxPrice'] = $maxPrice;
			$data['minPrice'] = $minPrice;
	}

}

?>
